package tp.pr5.items;

import tp.pr5.NavigationModule;
import tp.pr5.RobotEngine;
import tp.pr5.Street;

/**
 * A CodeCard can open or close the door placed in the streets.
 *  The card contains a code that must match the street code in order to perform the action.
 *
 */
public class CodeCard extends Item
{
	
	private String code;	
	
	/**
	 * Code card constructor
	 * 
	 * @param id - Code card name
	 * @param description - Code card description
     * @param code - Secret code stored in the code card
	 */
	
	public CodeCard(String id, String description, String code)
	{
		super(id, description);
		this.code = code;
	}
	
	/**
	 *A code card always can be used. Since the robot has the code card it never loses it 
	 */
	
	public boolean canBeUsed()
	{
		return true;
	}
	
	/**
	 * The method to use a code card. If the robot is in a place which contains a street in the direction 
	 * he is looking at,then the street is opened or closed if the street code and the card code match.
	 * 
	 * @param r - the robot engine employed to use the card.
     * @param p - the place where the card is going to be used
     * 
     * @return true If the code card can complete the action of opening or closing a street. 
     * Otherwise it returns false.
	 */
	
	public boolean use(RobotEngine r, NavigationModule nav)
	{		
		Street headingStreet = nav.getHeadingStreet();
		
		if(headingStreet != null)
		{
			if(headingStreet.getCode() != null)
			{
				if(headingStreet.getCode().equals(this.code))
			    {
					if(!headingStreet.isOpen())
				    {
						headingStreet.open(this);
						return true;
				    }
					else 
					{
						headingStreet.close(this);
						return true;
					}
			    }
			}
			return false;
		}
		return false;
	}
	
	/**
	 * Gets the code stored in the code card
	 * @return A String that represents the code
	 */
	
	public String getCode()
	{
		return this.code;
	}

	@Override
	public boolean canBeDefused() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int explode() {
		// TODO Auto-generated method stub
		return 0;
	}
}
